package com.landeb.shop.huo.act;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.landeb.shop.R;
import com.landeb.shop.huo.MyApplication;
import com.landeb.shop.huo.bean.BeanActCreateOrderResult;
import com.landeb.shop.huo.httpRequest.ChargeActivityRequest;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ChargeActivity extends BaseActivity {

    private Button btnChargeLog, btnCharge;
    private EditText etMoneyCharge;
    private LinearLayout btnBack;
    private OkHttpClient client;
    private Retrofit mRetrofit;
    private ChargeActivityRequest mRequset;

    public static void toAct(Context context) {

        context.startActivity(new Intent(context, ChargeActivity.class));

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_charge);

        client = MyApplication.getClient();
        String mUrl = MyApplication.BASE_URL;
        mRetrofit = new Retrofit.Builder()
                .baseUrl(mUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(client)
                .build();

        mRequset = mRetrofit.create(ChargeActivityRequest.class);

        btnChargeLog = (Button) findViewById(R.id.btn_changeLog_charge_act);
        btnBack = (LinearLayout) findViewById(R.id.btn_back_charge_act);
        btnCharge = (Button) findViewById(R.id.btn_pay_charge_act);
        etMoneyCharge= (EditText) findViewById(R.id.et_money_charge_act);

        btnChargeLog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RechargeLogActivity.toAct(ChargeActivity.this);
            }
        });

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        btnCharge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (TextUtils.isEmpty(etMoneyCharge.getText().toString())){
                    Toast.makeText(ChargeActivity.this,"请输入正确的金额",Toast.LENGTH_SHORT).show();
                    return;
                }


                final ProgressDialog dialog = new ProgressDialog(ChargeActivity.this);
                dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                dialog.setMessage("正在提交，请稍后");
                dialog.setCancelable(false);
                dialog.show();

                mRequset.chargeMoney(etMoneyCharge.getText().toString())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Subscriber<BeanActCreateOrderResult>() {
                            @Override
                            public void onCompleted() {
                                Log.e("chargeMoney", "onCompleted: ");
                                dialog.dismiss();
                            }

                            @Override
                            public void onError(Throwable e) {
                                Log.e("chargeMoney", "onError: " + e.toString());
                                dialog.dismiss();
                            }

                            @Override
                            public void onNext(BeanActCreateOrderResult beanActCreateOrderResult) {

                                if (beanActCreateOrderResult.getStatus() == 1) {

                                    String orderId = beanActCreateOrderResult.getData().getId();

                                    PayActivity.toAct(ChargeActivity.this, orderId);

                                    finish();

                                } else {
                                    Toast.makeText(ChargeActivity.this, beanActCreateOrderResult.getInfo(), Toast.LENGTH_SHORT).show();
                                }

                            }
                        });


            }
        });
    }
}
